package pers.fulsun.es.test._03_doc;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import pers.fulsun.es.test._01_ESClient;
import pers.fulsun.es.test.entity.User;

public class _01_ESTest_Doc_Insert {
  private static RestHighLevelClient client = _01_ESClient.getClient();

  public static void main(String[] args) throws IOException {
    // 新增文档 - 请求对象
    IndexRequest request = new IndexRequest();
    // 设置索引及唯一性标识
    request.index("user").id("1001");
    // 创建数据对象
    User user = new User();
    user.setName("zhangsan");
    user.setAge(30);
    user.setSex("男");
    ObjectMapper objectMapper = new ObjectMapper();
    String productJson = objectMapper.writeValueAsString(user);
    // 添加文档数据，数据格式为 JSON 格式
    request.source(productJson, XContentType.JSON);
    // 客户端发送请求，获取响应对象
    IndexResponse response = client.index(request, RequestOptions.DEFAULT);
    //// 3.打印结果信息
    System.out.println("_index:" + response.getIndex());
    System.out.println("_id:" + response.getId());
    System.out.println("_result:" + response.getResult());
    // 关闭客户端连接
    client.close();
  }
}
